#include "diff2d.h"

#include <vector>
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

void diff2D::diffList::ptAppend(int i,int j,double coef)
{
    short lud=0;
    pt ptNew;
    ptNew.i=i;
    ptNew.j=j;

    if(i<0) lud=-1;
    else if(i==0){
        if(j<0) lud=-1;
        else if(j>0) lud=1;
    }
    else if(i>0) lud=1;

    if(lud==-1){
        lNum++;
        lList.push_back(ptNew);
        lCoef.push_back(coef);
    }
    else if(lud==1){
        uNum++;
        uList.push_back(ptNew);
        uCoef.push_back(coef);
    }
    else{
        dNum++;
        dList.push_back(ptNew);
        dCoef.push_back(coef);
    }
    return;
}

void diff2D::diffList::minus(diff2D::diffList aList)
{
    bool bk;

    for(int p=0;p<aList.lNum;p++){
        bk=0;
        for(int q=0;q<lNum;q++){
            if(aList.lList[p].i==lList[q].i&&aList.lList[p].j==lList[q].j){
                lCoef[q]-=aList.lCoef[p];
                bk=1;
                break;
            }
        }
        if(bk==0){
            lList.push_back(aList.lList[p]);
            lCoef.push_back(-aList.lCoef[p]);
            lNum++;
        }
    }

    for(int p=0;p<aList.uNum;p++){
        bk=0;
        for(int q=0;q<uNum;q++){
            if(aList.uList[p].i==uList[q].i&&aList.uList[p].j==uList[q].j){
                uCoef[q]-=aList.uCoef[p];
                bk=1;
                break;
            }
        }
        if(bk==0){
            uList.push_back(aList.uList[p]);
            uCoef.push_back(-aList.uCoef[p]);
            uNum++;
        }
    }

    for(int p=0;p<aList.dNum;p++){
        bk=0;
        for(int q=0;q<dNum;q++){
            if(aList.dList[p].i==dList[q].i&&aList.dList[p].j==dList[q].j){
                dCoef[q]-=aList.dCoef[p];
                bk=1;
                break;
            }
        }
        if(bk==0){
            dList.push_back(aList.dList[p]);
            dCoef.push_back(-aList.dCoef[p]);
            dNum++;
        }
    }
    return;
}

void diff2D::diffList::add(diff2D::diffList aList)
{
    diff2D::diffList maList;
    maList.minus(aList);
    minus(maList);
    return;
}

void diff2D::diffList::times(double am)
{
    for(int p=0;p<lNum;p++)
        lCoef[p]*=am;
    for(int p=0;p<uNum;p++)
        uCoef[p]*=am;
    for(int p=0;p<dNum;p++)
        dCoef[p]*=am;
    return;
}

void diff2D::diffList::divided(double ad)
{
    times(1/ad);
    return;
}

void diff2D::diffList::print()
{
    cout<<"Diff mat:"<<endl;
    cout<<"\tL Pts:"<<endl;
    for(int k=0;k<lNum;k++){
        cout<<"\t\t"<<lList[k].i<<'\t'<<lList[k].j<<'\t'<<lCoef[k]<<endl;
    }
    cout<<"\tD Pts:"<<endl;
    for(int k=0;k<dNum;k++){
        cout<<"\t\t"<<dList[k].i<<'\t'<<dList[k].j<<'\t'<<dCoef[k]<<endl;
    }
    cout<<"\tU Pts:"<<endl;
    for(int k=0;k<uNum;k++){
        cout<<"\t\t"<<uList[k].i<<'\t'<<uList[k].j<<'\t'<<uCoef[k]<<endl;
    }
    cout<<endl;
    return;
}

